﻿/*  ----------------------------------------------------------------------------
 *  DES-Release 2
 *  ----------------------------------------------------------------------------
 *  File:       Form1.cs
 *  Author:     nitinkoshy@gmail.com
 *  Source:     http://des.codeplex.com/
 *  ----------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DES.Common;
using System.ServiceModel;
using System.Reflection;
using System.IO;
using System.Messaging;
using System.Security.Principal;
using DES.Task.Basic;

namespace DES.Test.ChildTasks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //for (int numberOfRequest = 0; numberOfRequest < nudRequests.Value; numberOfRequest++)
            SimulateRequest();
        }

        private void SimulateRequest()
        {
            ChannelFactory<IDESService> desServiceFactory = new ChannelFactory<IDESService>(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:800/DESService"));
            IDESService desService = desServiceFactory.CreateChannel();


            ClientTask clientTask = CreateClientTask(null,tbPrefix.Text+"PARENT");

            for (int i = 0; i < nudChildCount.Value; i++)
            {
                ClientTask childTask = CreateClientTask(clientTask.Id, tbPrefix.Text + "CHILD_" + i.ToString());
                for (int j = 0; j < nudGrandChildCount.Value; j++)
                {
                    ClientTask grandchildTask = CreateClientTask(childTask.Id, tbPrefix.Text + "GRAND_" + i.ToString() + "_" + j.ToString());
                    childTask.ChildTasks.Add(grandchildTask);
                }

                clientTask.ChildTasks.Add(childTask);

            }

            clientTask.EndPoint = ".\\private$\\ResultQueue-App-A";
            desService.ProcessTask(clientTask);

            desServiceFactory.Close();

        }

        ClientTask CreateClientTask(Guid? parentTaskId, string name)
        {
            Type type = new AddNumbersTask().GetType();
            //Assembly assembly = Assembly.GetAssembly(type);
            //byte[] assemblyBytes = File.ReadAllBytes(assembly.Location);

            ClientTask clientTask;
            if (parentTaskId.HasValue)
              clientTask = new ClientTask(parentTaskId.Value);
            else
              clientTask = new ClientTask();

            clientTask.TypeName = type.FullName;
            clientTask.CanExecuteIndependently = true;
            clientTask.AssemblyFileLocation = @"D:\Work\MSCS\Project\DES\Tasks\DES.Task.Basic\bin\Debug\DES.Task.Basic.dll";
            //clientTask.AssemblyBytes = assemblyBytes;
            clientTask.AssemblyName = "DES.Task.Basic";
            clientTask.Id = Guid.NewGuid();
            clientTask.Credential = new Credential("nitinstephen_koshy", "NOKIAn70m0b1l8", "itlinfosys");
            clientTask.Name = name;
            //clientTask.Credential = new Credential("tester", "W1nd0w*V1*ta", "blrkec37146l");

            return clientTask;
        }
    }
}
